﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.IO;

namespace ConsoleServer
{
    class Program
    {
        private static KinectGestureDetector kgd;
        private static MainServerThread mst;
        private 
        static void Main(string[] args)
        {
            Console.WriteLine("KinectEventServer v0.1");
            Console.WriteLine("Connecting Kinect...");
            kgd = new KinectGestureDetector();
            new Thread(new ThreadStart(kgd.setUpAndRun)).Start(); ;            
            Console.WriteLine("Kinect thread started");
            Console.WriteLine("Starting server main thread");
            mst = new MainServerThread(kgd);
            new Thread(new ThreadStart(mst.awaitConnections)).Start();

            String command = "";
            while (true)
            {
                Console.WriteLine("Enter command: ");
                command = Console.ReadLine();
                String[] input = command.Split(' ');
                switch (input[0])
                {
                    case "QUIT":
                        Environment.Exit(0);
                        break;
                    case "HELP":
                        usage();
                        break;
                    case "LISTCLIENTS":
                        listclients();
                        break;
                    case "SEND":
                        sendCmd(input);
                        break;
                    case "RUNSCRIPT":
                        runscript(input);
                        break;
                    default:
                        Console.WriteLine("Unknown command \"" + command + "\"");
                        break;
                }
            }
        }

        private static void runscript(String[] input)
        {
            if (input.Length > 1)
            {
                String file = input[1];
                try
                {
                    String[] contents = File.ReadAllLines(file);
                    foreach (String s in contents)
                    {
                        String[] tokens = s.Split(new char[] { ' ' }, 2);
                        switch (tokens[0])
                        {
                            case "SEND":
                                
                                Console.WriteLine("Sending '" + tokens[1] + "'");
                                mst.distribute(tokens[1]);
                                break;
                            case "WAIT":

                                try
                                {
                                    int waittime = Convert.ToInt16(tokens[1]);
                                    Console.WriteLine("Waiting " + waittime + "ms");
                                    Thread.Sleep(waittime);
                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("Error: " + tokens[1] + " is not a timespan.");
                                }                                
                                
                                break;
                            default:
                                Console.WriteLine("Invalid command in script: " + tokens[0]);
                                break;
                        }
                        
                    }

                    
                }
                catch (Exception)
                {
                    Console.WriteLine("Error");
                }
            }
            else
            {
                Console.WriteLine("Bad command");
            }
        }
        private static void sendCmd(String[] input)
        {
            if (input.Length > 1)
            {
                String cmd = input[1];
                mst.distribute(cmd);
            }
            else
            {
                Console.WriteLine("Bad command");
            }
        }
        private static void listclients()
        {
            ArrayList connections = mst.getConnections();
            foreach (ClientConnectionHandler cch in connections)
            {
                Console.WriteLine("Client: " + cch.getRemoteAddress());
                Console.WriteLine("Subscribed to: " + String.Concat(cch.getSubscribtions()));
                Console.WriteLine("--------------------------------------------------------");
            }
        }
        private static void usage()
        {
            Console.WriteLine("\nQUIT\tEnd the server");
            Console.WriteLine("\nLOAD <filename>\tLoad and execute script file with commands");
            Console.WriteLine("\nLISTCLIENTS\tLists the connected clients\n");
        }
    }
}
